getADValue

Owner: Michelle

Purpose and Documentation

What is the purpose of this function? Explain what it does, and justify it's place in the program. This section will appear in the Technical Documentation, so try to be complete.

Syntax

int getADValue()

Usage

When and how should this function be called? Use examples where necessary.

Input Arguments

  • void. This function takes no arguments.
  • int example. A value that is used by the function.
  • char* string. A string that will be transmitted to the serial transmit.

Output Arguments

  • None. This function does not make changes to any existing values.
  • int* status. The program will modify the non-local value status if it has an error.

Return Value

  • int. When the operation successfully, a 1 is returned. Otherwise, a 0 is returned.

Settings and Registers

Read

  • int Settings.speech_mute. Needed to see if the string should be spoken.
  • PORTA <0-4>: Please add the names of the registers you use to the "tags" of this page for indexing purposes.
  • PIE1<3>

Write

  • int Settings.current_state. The program will change the operating state of the program.
  • INTCON1

Operation

Describe in very general terms how the function fulfills it's purpose. This should be reflected in the code.

Code

/**
* Basic weighing methods
*/
 
//Includes
#include <p18f452.h>
 
//#defines
#define GRAMS 0
#define OUNCES 1
 
//Prototypes
void initialiseADConverter();
int getADValue();
 
//Global variables
//TODO: Some of these variables will be replaced with a struct
char units = 0; //Store the unit, GRAMS or OUNCES. Defaults to grams.
 
#pragma code
 
void main (void)
{
    initialiseADConverter();
 
}
 
/**
 * Intitialises the A/D converter, and waits until the A/D converter has finished enabling.
 * Postcondition: The A/D converter is enabled.
 */
void initialiseADConverter()
{
    ADCON0 = 0b01000001; //Fosc/8, A/D enabled
    ADCON1 = 0b10001110; //Right justify, 1 analog channel; VDD and VSS references
    T0CON  = 0b11000111; //TMR0 prescaler, 1:256
    while(INTCONbits.TMR0IF == 0);
    INTCONbits.TMR0IF = 0;
}
 
/**
 * Returns an unsigned 16 bit integer, containing 10 bits of A/D result. The 6 most
 * significant bits will be 0.
 * Precondition: The A/D Converter is on and initialised
 * @return 10 bit A/D result
 */
unsigned int getADValue()
{
    ADCON0bits.GO = 1;    //Start A/D conversion
    while(PIR1bits.ADIF == 0);    //Wait until ready
 
    int result = ADRESH << 8;    //Get upper two bits
    result = result | ADRESL;    //Get lower eight bits
    return result;                //Result is 10 bits
}